package com.lanou3g.k12.web.controller;

import com.lanou3g.k12.entity.Event;
import com.lanou3g.k12.entity.EventCategory;
import com.lanou3g.k12.entity.EventContent;
import com.lanou3g.k12.service.EventCategoryService;
import com.lanou3g.k12.service.EventContentService;
import com.lanou3g.k12.service.EventService;
import com.lanou3g.k12.utils.K12Result;
import com.lanou3g.k12.utils.K12ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
public class EventCategoryController {
    @Autowired
    private EventCategoryService eventCategoryService;
    @Autowired
    private EventService eventService;
    @Autowired
    private EventContentService eventContentService;

    //    添加类别
    @PostMapping("/eventCategory")
    public K12Result addEventCategory(EventCategory eventCategory) {
        try {
            eventCategoryService.addEventCategory(eventCategory);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }
    }

    //   对活动分类管理的禁用
    @PutMapping("/eventCategory/{id}")
    public K12Result updateEventCategoryStatus(@PathVariable Long id) {
        try {
            eventCategoryService.updateEventCategoryStatus(id);
            eventService.updateEventStatus(id);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }

    }

    //   对活动分类管理的删除
    @DeleteMapping("/eventCategory/{id}")
    public K12Result deleteCategoryById(@PathVariable Long id) {
        try {
            List<Event> eventList = eventService.findEventByCid(id);
            for (Event event : eventList) {
                eventContentService.deleteEventContentById(event.getId());
            }
            eventService.deleteEventByCid(id);
            eventCategoryService.deleteCategoryById(id);
            return K12ResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return K12ResultUtils.error(533);
        }

    }
}
